from rest_framework import serializers
from .models import AncientPoetry, AncientBooks


class ContentListField(serializers.Field):
    def to_representation(self, value):
        return value.replace('&', '')  # 去掉&分割符号


class AncientPoetrySerializer(serializers.ModelSerializer):
    content = ContentListField()  # 序列化字符串

    class Meta:
        model = AncientPoetry
        # fields = '__all__'
        exclude = ['appreciation', 'notes']
        read_only_fields = ['id']


class PicURLField(serializers.Field):
    def to_representation(self, value):
        return 'http://127.0.0.1:9000/static/images/ancient-books/' + value


class AncientBooksSerializer(serializers.ModelSerializer):
    pic_url = PicURLField()  # 序列化字符串

    class Meta:
        model = AncientBooks
        fields = ['id', 'title', 'pic_url', 'introduce']
        # exclude = ['content']
        read_only_fields = ['id']
